package com.integral.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.integral.biz.IUserRelationBiz;
import com.integral.biz.IUserTypeBiz;
import com.integral.entity.UserInfo;
import com.integral.entity.UserRelation;
import com.integral.vo.UserRelationVo;
import com.integral.vo.UserTypeVo;

@Controller
@RequestMapping("/relation")
public class relationController {
	@Resource
	private IUserRelationBiz userRelationBiz;
	@Resource
	private IUserTypeBiz userTypeBiz;
	
	/**
	 * 查询结算关系列表
	 * @param request
	 * @param response
	 * @param userRelationVo
	 * @return
	 */
	@RequestMapping(value="/findUserRelation")
	@ResponseBody
	public Map<String,Object> findUserRelation(HttpServletRequest request, HttpServletResponse response,UserRelationVo userRelationVo){
		//设置查询未删除的数据
		userRelationVo.setDeleteFlag(0);
		List<UserRelationVo> list = userRelationBiz.findUserRelation(userRelationVo);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("total", userRelationBiz.findUserRelationCount(userRelationVo));
		map.put("rows", list);
		return map;
	}
	/**
	 * 加载用户类别下拉框
	 * @return
	 */
	@RequestMapping(value="/loadUserType")
	@ResponseBody
	public List<UserTypeVo> loadUserType(){
		//创建查询对象
		UserTypeVo userTypeVo = new UserTypeVo();
		userTypeVo.setStart(null);
		userTypeVo.setPageSize(null);
		userTypeVo.setDeleteFlag(0);
		List<UserTypeVo> list = userTypeBiz.findUserType(userTypeVo);
		return list;
	}
	/**
	 * 新增结算关系
	 * @param userRelationVo
	 * @return
	 */
	@RequestMapping(value="/addUserRelation")
	public String addUserRelation(UserRelationVo userRelationVo){
		UserRelation userRelation = new UserRelation();
		//上级类别编码
		userRelation.setSuperiorTypeCode(userRelationVo.getSuperiorTypeCode());
		//下级类别编码
		userRelation.setSubordinateTypeCode(userRelationVo.getSubordinateTypeCode());
		//佣金金额
		userRelation.setMoney(userRelationVo.getMoney());
		//结算关系
		userRelation.setRelationshipClearing(userRelationVo.getRelationshipClearing());
		userRelationBiz.addUserRelation(userRelation);
		return "management/relation";
	}
	/**
	 * 删除关系
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/delRelation")
	@ResponseBody
	public String delRelation(HttpServletRequest request,Integer id){
		HttpSession session = request.getSession();
		UserInfo user = (UserInfo)session.getAttribute("userInfo");
		
		UserRelation userRelation = new UserRelation();
		userRelation.setId(id);
		userRelation.setDeleteFlag(1);
		userRelation.setUpdateEmp(user.getUserName());
		userRelation.setUpdateTime(new Date());
		int i = userRelationBiz.updateUserRelation(userRelation);
		if(i > 0){
			return "删除成功！";
		}else{
			return "删除失败！";
		}
	}
	/**
	 * 修改关系
	 * @param request
	 * @param userRelationVo
	 * @return
	 */
	@RequestMapping(value="/updateRelation")
	@ResponseBody
	public String updateRelation(HttpServletRequest request,UserRelationVo userRelationVo){
		HttpSession session = request.getSession();
		UserInfo user = (UserInfo)session.getAttribute("userInfo");
		
		UserRelation userRelation = new UserRelation();
		userRelation.setId(userRelationVo.getId());
		userRelation.setMoney(userRelationVo.getMoney());
		userRelation.setRelationshipClearing(userRelationVo.getRelationshipClearing());
		userRelation.setSubordinateTypeCode(userRelationVo.getSubordinateTypeCode());
		userRelation.setSuperiorTypeCode(userRelationVo.getSuperiorTypeCode());
		userRelation.setUpdateEmp(user.getUserName());
		userRelation.setUpdateTime(new Date());
		int i = userRelationBiz.updateUserRelation(userRelation);
		if(i > 0){
			return "修改成功！";
		}else{
			return "修改失败！";
		}
	}
}
